﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.ScriptEngine.Custom;
using DWF.Studio.ScriptEngine.Event;
using DWF.Studio.ScriptEngine.IPC.Msg;
using System;
using System.Activities;
using System.Activities.Debugger;
using System.Activities.Tracking;
using System.Activities.XamlIntegration;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;

namespace DWF.Studio.ScriptEngine.Implementation
{
    internal class WorkflowScriptEngineSub : WorkflowScriptEngine
    {
        public WorkflowScriptEngineSub(Custom.ExecutionContext excutionContext) :
           base(excutionContext)
        {
        }

        public override void ExecuteScript(string projectDirectory, string scriptPath, Dictionary<string, object> param)
        {
            this.ResultBlock.Reset();
            this.InnerException = null;
            this.ExcuteResult = null;

            try
            {
                Excute(projectDirectory, scriptPath, param);
            }
            catch (Exception ex)
            {
                InnerException = ex;
                SendUpwardMsg<ExecuteUnhandledExceptionEventArgs>(new ExecuteUnhandledExceptionEventArgs() { ScriptPath = scriptPath, TypeName = ex.GetType().FullName, StackTrace = ex.StackTrace, Message = ex.Message }, EMsgType.ExcuteUnhandledException);
            }
            finally
            {
                SendUpwardMsg<EventArgs>(EventArgs.Empty, EMsgType.UnloadSubEngine);
                ExcutionContext.Pop(this);
                ResultBlock.Continue();
            }
        }

        public override void DebugScript(string projectDirectory, string scriptPath, Dictionary<string, object> param)
        {
            this.ResultBlock.Reset();
            this.InnerException = null;
            this.ExcuteResult = null;

            try
            {
                Debug(projectDirectory, scriptPath, param);
            }
            catch (Exception ex)
            {
                InnerException = ex;
                SendUpwardMsg<ExecuteUnhandledExceptionEventArgs>(new ExecuteUnhandledExceptionEventArgs() { ScriptPath = scriptPath, TypeName = ex.GetType().FullName, StackTrace = ex.StackTrace, Message = ex.Message }, EMsgType.ExcuteUnhandledException);
            }
            finally
            {
                SendUpwardMsg<EventArgs>(EventArgs.Empty, EMsgType.UnloadSubEngine);
                ExcutionContext.Pop(this);
                ResultBlock.Continue();
            }
        }
    }
}
